<template>
  <div v-loading="loading" class="AddOrUpdate_main add_container column_one">
    <div class="main_main">
      <el-form ref="form" :model="formData" :rules="rules" :inline="true">
        <el-form-item label="货架编码" prop="shelfCode">
          <el-input v-model="formData.shelfCode" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="货架名称" prop="shelfName">
          <el-input v-model="formData.shelfName" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="货架标签" prop="lightMac">
          <el-input v-model="formData.lightMac" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="蓝牙网关" prop="lightMac">
          <!--          <el-input v-model="formData.hostName" placeholder="请输入" clearable />-->
          <el-select v-model="formData.hostName" filterable clearable placeholder="请选择">
            <el-option
              v-for="item in hostNameOptions"
              :key="item.hostName"
              :label="item.hostName"
              :value="item.hostName"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script>
import { addOrUpdate, getInfo } from '@/api/warehouseManagement/shelfManagement'
import { list } from '@/api/superManage/iotManage/iotConfiguration'
export default {
  name: 'AddOrUpdate',
  props: {
    layerid: {},
    id: {},
    defaultValue: {}
  },
  data() {
    return {
      loading: true,
      isShow: false,
      formData: {
        id: '',
        shelfCode: '',
        shelfName: '',
        lightMac: '',
        hostName: ''
      },
      roleOptions: [],
      hostNameOptions: [],
      rules: {
        shelfName: [
          { required: true, message: '货架名称不能为空', trigger: 'change' }
        ],
        shelfCode: [
          { required: true, message: '货架编码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.gethostNameInfo()
    if (!this.id) { // 没有id就是新增
      this.loading = false
      Object.keys(this.defaultValue).forEach(key => { // 给右边数据赋值
        this.formData[key] = this.defaultValue[key]
      })
    } else { // 有id就是编辑
      this.getInfo()
    }
  },
  methods: {
    getInfo() {
      getInfo(this.id).then(res => {
        Object.keys(this.formData).forEach(key => { // 给右边数据赋值
          this.formData[key] = res.data[key]
        })
        this.isShow = true
        this.loading = false
      })
    },
    gethostNameInfo() {
      list({ iotType: '蓝牙网关' }).then(res => {
        this.hostNameOptions = res.data
        if (this.hostNameOptions.length == 1) {
          this.formData.hostName = this.hostNameOptions[0].hostName
        }
      })
    },
    onSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          addOrUpdate(this.formData).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.$layer.close(this.layerid) // 关闭弹框
            this.$parent.getList()
            this.$parent.refresh() // 调用父组件的刷新方法
          })
        }
      })
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    }
  }
}
</script>

<style lang="less" scoped>
</style>
